﻿using System;
using System.Collections.Generic;
using System.Linq;
using Yxinmt.Interface;
using Yxinmt.Utility;

namespace Yxinmt.Windows.Meeting
{
    public class InkCanvasPropertyMenu
    {
        IControl _StrokeWidth { get; set; }
        IControl _FillType { get; set; }

        List<IControl> _ToolBarSelectButtonList { get; set; }
        IWindow _Window { get; set; }

        Controls.ImageTextButton _SetColor { get; set; }

        public InkCanvasPropertyMenu(IWindow window, InkCanvasProperty property)
        {
            _Window = window;
            Property = property;

            InitLoad();
        }

        public InkCanvasProperty Property { get; set; }

        void InitLoad()
        {
            _SelectFontFamily = "宋体";
            _SelectFontSize = 24;

            _ToolBarSelectButtonList = new List<IControl>();

            var panel = _Window.ComponentList.Where(w => w.Name == "RightPropertyPanel2").FirstOrDefault();

            var controlList = panel.GetComponentsControls();
            var setColorPanel = panel.ComponentList.Where(w => w.Name == "SetColorPanel").FirstOrDefault();
            controlList.AddRange(setColorPanel.GetComponentsControls());

            controlList.ForEach(c =>
            {
                if (c.Name == "StrokeWidth") _StrokeWidth = c;
                else if (c.Name == "FillType") _FillType = c;
                else if (c.Name == "SetColor")
                {
                    _SetColor = (Controls.ImageTextButton)c;
                }
                if (c.Property.GetValue<bool>("IsSelectButton")) _ToolBarSelectButtonList.Add(c);

                if (c is Controls.ImageTextButton btn)
                {
                    btn.ClickAction = (obj, e, ctl) => setClickAction(ctl);
                }
            });

            SetComboBox();

            _SelectToolBarButton = _ToolBarSelectButtonList[0];
        }

        void setClickAction(IControl ctl)
        {
            if (ctl.Property.GetValue<bool>("IsSelectButton"))
            {
                SetSetToolBarSelectButton(ctl.Name);
            }
            else if (ctl.Name == "SetFont") SetSetFont();
            else if (ctl.Name == "SetColor") SetSetColor();
            else if (ctl.Name == "LastCancel") SetLastCancel();
            else if (ctl.Name == "NextCancel") SetNextCancel();
            else if (ctl.Name == "Clear") SetClear();
        }

        private void SetComboBox()
        {
            List<int> itemList = new List<int>();
            for (int i = 1; i < 31; i++) itemList.Add(i);

            Controls.ComboBox cb = (Controls.ComboBox)_StrokeWidth;
            cb.SelectionChanged = (selectItem) => SetStrokeWidthChanged(selectItem);
            cb.SetDateSource(itemList);
            cb.SetValue(2);

            List<string> typeList = new List<string>() { "空心", "实心" };

            cb = (Controls.ComboBox)_FillType;
            cb.SelectionChanged = (selectItem) => SetFillTypeChanged(selectItem);
            cb.SetDateSource(typeList);
            cb.SetValue("空心");
        }

        private void SetStrokeWidthChanged(object value)
        {
            SetStrokeWidth();
        }

        private void SetStrokeWidth()
        {
            double width = 0;
            object value = _StrokeWidth.GetValue();

             if (value != null && double.TryParse(value.ToString(), out width) && Property != null) Property.StrokeWidth(width);
        }


        private void SetFillTypeChanged(object value)
        {
            if (value != null && Property != null) Property.IsFill(value.ToString() == "实心");
        }

        void SetSelectToolBarButton()
        {
            _SelectToolBarButton.SetBackColor(_SelectToolBarButton.Property.GetStringValue("SelectedBackColor"));
        }

        public void DataLoad()
        {
            SetSelectToolBarButton();
            SetInkCanvasProperty();
        }

        string _SelectFontFamily { get; set; }
        bool _SelectItalic { get; set; }
        bool _SelectBold { get; set; }
        float _SelectFontSize { get; set; }

        public void SetSetFont()
        {
            System.Windows.Forms.FontDialog dialog = new System.Windows.Forms.FontDialog();
            dialog.Font = new System.Drawing.Font(_SelectFontFamily, _SelectFontSize, GetFontStyle(), System.Drawing.GraphicsUnit.Point);
            dialog.ShowEffects = false;

            _Window.TaskRun("SetSetFont", true, () =>
            {
                _Window.Window.Dispatcher.Invoke(() =>
                {
                    try
                    {
                        if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            _SelectFontFamily = dialog.Font.FontFamily.Name;
                            _SelectBold = dialog.Font.Bold;
                            _SelectItalic = dialog.Font.Italic;
                            _SelectFontSize = dialog.Font.Size;

                            if (Property != null) Property.TextFont(_SelectFontFamily, _SelectFontSize, _SelectBold, _SelectItalic);
                        }
                    }
                    catch (Exception ex)
                    {
                        _Window.Alert(_Window.Name, "SetSetFont", ex);
                    }
                });
            });
        }

        private System.Drawing.FontStyle GetFontStyle()
        {
            if (_SelectBold && _SelectItalic) return System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic;
            else if (_SelectItalic) return System.Drawing.FontStyle.Italic;
            else if (_SelectBold) return System.Drawing.FontStyle.Bold;
            else return System.Drawing.FontStyle.Regular;
        }

        string _SelectColor { get; set; }
        public void SetSetColor()
        {
            System.Windows.Forms.ColorDialog dialog = new System.Windows.Forms.ColorDialog();
            if (Property != null) dialog.Color = Utility.Common.GetDrawingColor(Property.GetColor());

            _Window.TaskRun("SetSetColor", true, () =>
            {
                _Window.Window.Dispatcher.Invoke(() =>
                {
                    try
                    {
                        if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            Color = System.Drawing.ColorTranslator.ToHtml(dialog.Color);
                        }
                    }
                    catch (Exception ex)
                    {
                        _Window.Alert(_Window.Name, "SetSetColor", ex);
                    }
                });
            });
        }

        public string Color
        {
            get
            {
                return _SelectColor;
            }
            set
            {
                _SelectColor = value;
                if (Property != null) Property.Color(value);

                _SetColor.SetLabelBackColor(value);
            }
        }


        IControl _SelectToolBarButton { get; set; }

        public void SetSetToolBarSelectButton(string name)
        {
            _ToolBarSelectButtonList.ForEach(b =>
            {
                if (b.Name == name)
                {
                    b.SetBackColor(b.Property.GetStringValue("SelectedBackColor"));
                    _SelectToolBarButton = b;
                }
                else b.SetBackColor(b.Property.GetStringValue("BackColor"));
            });

            switch (name)
            {
                case "Circle": _ShapeType = Controls.InkCanvasShapeType.Circle; break;
                case "Ellipse": _ShapeType = Controls.InkCanvasShapeType.Ellipse; break;
                case "Line": _ShapeType = Controls.InkCanvasShapeType.Line; break;
                case "Rect": _ShapeType = Controls.InkCanvasShapeType.Rect; break;
                case "Text": _ShapeType = Controls.InkCanvasShapeType.Text; break;
                default: _ShapeType = Controls.InkCanvasShapeType.Ink; break;
            }

            if (Property != null)
            {
                Property.ShapeType(_ShapeType);
                Property.SetCursor();
            }
        }

        Controls.InkCanvasShapeType _ShapeType { get; set; }

        public void SetInkCanvasProperty()
        {
            if (Property != null)
            {
                Property.ShapeType(_ShapeType);
                Property.SetCursor();
                Property.TextFont(_SelectFontFamily, _SelectFontSize, _SelectBold, _SelectItalic);
                Property.IsFill(_FillType.GetValue().ToString().Equals("实心"));
                Property.Color(_SelectColor);

                SetStrokeWidth();
            }
        }

        public void SetLastCancel()
        {
           if (Property != null) Property.Cancel();
        }

        public void SetNextCancel()
        {
            if (Property != null) Property.BackCancel();
        }

        public void SetClear()
        {
            if (Property != null) Property.Clear();
        }
    }
}
